import numpy as np
import csv
import random
from datetime import datetime
from time import strftime

'''
revision of test24
to calculate the false nagetive, false positive

'''

def getCurTime():
    """
    get current time
    Return value of the date string format(%Y-%m-%d %H:%M:%S)
    """
    format='%Y-%m-%d %H:%M:%S'
    sdate = None
    cdate = datetime.now()
    try:
        sdate = cdate.strftime(format)
    except:
        raise ValueError
    return sdate

def build_data_list():
    #print getCurTime()
    sKey = np.array([])
    fn = unitCSV    # input csv location
    ra = csv.DictReader(file(fn), dialect="excel")
    
    for record in ra:
        temp = np.array([])
        for i in range(1,1003):
            temp = np.append(temp, float(record[ra.fieldnames[i]]))
        #temp = [int(float(record["ID"])), int(float(record[cancerFN])), int(float(record[popFN])), float(record[cancerFN])/float(record[popFN]), 0.0]
        sKey = np.append(sKey, temp)

    sKey.shape = (1000,-1)
    return sKey

def build_unit_list(cancerFN):
    sKey = np.array([])
    #read contiguity file
    fn = unitCSV

    #print "Strat building csv lists..."

    ra = csv.DictReader(file(fn), dialect="excel")
    for record in ra:
        #print record[ra.fieldnames[0]], type(record[ra.fieldnames[-1]])
        temp = [int(float(record[cancerFN])),int(float(record[popFN])),float(record["Area"]), 0.0]
        #print temp
        sKey =np.append(sKey, temp)
    sKey.shape=(-1,4)
    return sKey

def build_region_list(inputCSV):
    fn = inputCSV
    ra = csv.DictReader(file(fn), dialect="excel")
    temp_list = np.array([])
    i = 0
    for record in ra:
        #print record[ra.fieldnames[0]], type(record[ra.fieldnames[-1]])
        temp_list = np.append(temp_list, int(float(record[ra.fieldnames[-1]])))
        i = i + 1
    return temp_list

def build_max_p_region_list(inputCSV):
    fn = inputCSV
    #ra = csv.DictReader(file(fn), dialect="excel")
    ra = csv.reader(file(fn, 'rb'))
    temp_list = np.array([])
    i = 0
    for record in ra:
        #print record[ra.fieldnames[0]], type(record[ra.fieldnames[-1]])
        #temp_list = np.append(temp_list, int(float(record[ra.fieldnames[-1]])))
        temp_list = np.append(temp_list, int(float(record[-1])))
        i = i + 1
    return temp_list

def cal_region_attri(region_id):
    dis_reg = np.unique(region_id)
    iLen = dis_reg.shape[0]
    temp_reg_attri = np.zeros((iLen, 4)) #[caner, pop, area, rate]

    # unit_attri = [cancer, pop, area, rate]

    i = 0
    for item in unit_attri:
        temp_reg_attri[int(region_id[i]),0] += item[0]
        temp_reg_attri[int(region_id[i]),1] += item[1]
        #temp_reg_attri[int(region_id[i]),2] + = item[2]
        i = i + 1

    for item in temp_reg_attri:
        item[3] = item[0]/item[1]

    return temp_reg_attri[:,3]


def cal_unit_rate():           
    #region_id = build_max_p_region_list(regionCSV)  
    region_id = build_region_list(regionCSV)
    reg_rate = cal_region_attri(region_id) # [rate]
    i = 0
    for item in unit_attri:
        item[3] = reg_rate[int(region_id[i])]
        i += 1 

def cal_riskare_attri():
    temp_popcancer = np.array([])
    temp_high_popcancer = np.array([0,0,0.0]) #[cancer, pop, area]
    temp_low_popcancer = np.array([0,0,0.0]) #[cancer, pop, area]
    
    temp_cancer, temp_pop, temp_area = cal_list_pop(H1)
    temp_popcancer = np.append(temp_popcancer, [temp_cancer, temp_pop, temp_area])
    temp_high_popcancer[0] = temp_high_popcancer[0] + temp_cancer
    temp_high_popcancer[1] = temp_high_popcancer[1] + temp_pop
    temp_high_popcancer[2] = temp_high_popcancer[2] + temp_area
    
    temp_cancer, temp_pop, temp_area = cal_list_pop(L2)
    temp_popcancer = np.append(temp_popcancer, [temp_cancer, temp_pop, temp_area])
    temp_low_popcancer[0] = temp_low_popcancer[0] + temp_cancer
    temp_low_popcancer[1] = temp_low_popcancer[1] + temp_pop
    temp_low_popcancer[2] = temp_low_popcancer[2] + temp_area
    
    temp_cancer, temp_pop, temp_area = cal_list_pop(H3)
    temp_popcancer = np.append(temp_popcancer, [temp_cancer, temp_pop, temp_area])
    temp_high_popcancer[0] = temp_high_popcancer[0] + temp_cancer
    temp_high_popcancer[1] = temp_high_popcancer[1] + temp_pop
    temp_high_popcancer[2] = temp_high_popcancer[2] + temp_area
    
    temp_cancer, temp_pop, temp_area = cal_list_pop(H4)
    temp_popcancer = np.append(temp_popcancer, [temp_cancer, temp_pop, temp_area])
    temp_high_popcancer[0] = temp_high_popcancer[0] + temp_cancer
    temp_high_popcancer[1] = temp_high_popcancer[1] + temp_pop
    temp_high_popcancer[2] = temp_high_popcancer[2] + temp_area
    
    temp_cancer, temp_pop, temp_area = cal_list_pop(L5)
    temp_popcancer = np.append(temp_popcancer, [temp_cancer, temp_pop, temp_area])
    temp_low_popcancer[0] = temp_low_popcancer[0] + temp_cancer
    temp_low_popcancer[1] = temp_low_popcancer[1] + temp_pop
    temp_low_popcancer[2] = temp_low_popcancer[2] + temp_area
    
    temp_cancer, temp_pop, temp_area = cal_list_pop(L6)
    temp_popcancer = np.append(temp_popcancer, [temp_cancer, temp_pop, temp_area])
    temp_low_popcancer[0] = temp_low_popcancer[0] + temp_cancer
    temp_low_popcancer[1] = temp_low_popcancer[1] + temp_pop
    temp_low_popcancer[2] = temp_low_popcancer[2] + temp_area
    
    temp_cancer, temp_pop, temp_area = cal_list_pop(H7)
    temp_popcancer = np.append(temp_popcancer, [temp_cancer, temp_pop, temp_area])
    temp_high_popcancer[0] = temp_high_popcancer[0] + temp_cancer
    temp_high_popcancer[1] = temp_high_popcancer[1] + temp_pop
    temp_high_popcancer[2] = temp_high_popcancer[2] + temp_area
    
    temp_popcancer.shape = (-1, 3)
    return temp_popcancer, temp_high_popcancer, temp_low_popcancer

def cal_list_pop(list):
    # calculate the total pop and cancer in the input list
    temp_pop = 0
    temp_cancer = 0
    temp_area = 0
    # unit_attri = [cancer, pop, area, rate]
    for item in list:
        temp_pop = temp_pop + unit_attri[item, 1]
        temp_cancer = temp_cancer + unit_attri[item, 0]
        temp_area = temp_area + unit_attri[item, 2]
    return temp_cancer, temp_pop, temp_area

def cal_TP():
    # [threshold, count_TP, count_FN, count_FP, count_TP/(FN+FP), threshold, pop_TP, pop_FN, pop_FP, pop_TP/(FN+FP)]
    high_measure = np.zeros(10)
    low_measure = np.zeros(10)
    sort_rate_unit_attri =  unit_attri[np.argsort(-unit_attri[:,3]),:]
    temp_id = -1
    temp_high_measure = np.zeros(10)
    temp_low_measure = np.zeros(10)

    #temp_h = np.array([])
    #temp_l = np.array([])
    
    for item in sort_rate_unit_attri: # unit_attri = [cancer, pop, area, rate, id]
        if int(item[4]) in high_risk_area_id:
            temp_high_measure[6] += item[returnID]
            temp_high_measure[1] += 1
        else:
            temp_high_measure[8] += item[returnID]
            temp_high_measure[3] += 1

        temp_high_measure[7] = high_riskarea_attri[returnID] - temp_high_measure[6]
        temp_high_measure[2] = len(high_risk_area_id) - temp_high_measure[1]
        temp_high_measure[0] = temp_high_measure[5] = item[3]
        temp_high_measure[9] = temp_high_measure[6]/(temp_high_measure[7] + temp_high_measure[8])
        temp_high_measure[4] = temp_high_measure[1]/(temp_high_measure[2] + temp_high_measure[3])

        if temp_high_measure[9] > high_measure[9]:
            high_measure[5:] = temp_high_measure[5:]
        if temp_high_measure[4] > high_measure[4]:
            high_measure[0:5] = temp_high_measure[0:5]

        #temp_h = np.append(temp_h, temp_high_measure)            

    sort_rate_unit_attri =  unit_attri[np.argsort(unit_attri[:,3]),:]
    
    for item in sort_rate_unit_attri: # unit_attri = [cancer, pop, area, rate, id]
        if int(item[4]) in low_risk_area_id:
            temp_low_measure[6] += item[returnID]
            temp_low_measure[1] += 1
        else:
            temp_low_measure[8] += item[returnID]
            temp_low_measure[3] += 1

        temp_low_measure[7] = low_riskarea_attri[returnID] - temp_low_measure[6]
        temp_low_measure[2] = len(low_risk_area_id) - temp_low_measure[1]
        temp_low_measure[0] = temp_low_measure[5] = item[3]
        temp_low_measure[9] = temp_low_measure[6]/(temp_low_measure[7] + temp_low_measure[8])
        temp_low_measure[4] = temp_low_measure[1]/(temp_low_measure[2] + temp_low_measure[3])

        if temp_low_measure[9] > low_measure[9]:
            low_measure[5:] = temp_low_measure[5:]
        if temp_low_measure[4] > low_measure[4]:
            low_measure[0:5] = temp_low_measure[0:5]
        #temp_l = np.append(temp_l, temp_low_measure)
    #temp_h.shape = (-1, 10)
    #temp_l.shape = (-1, 10)
    
            
    temp_count_measure = np.array([repeat])
    temp_pop_measure = np.array([repeat])
    temp_count_measure = np.append(temp_count_measure, high_measure[0:5])
    temp_count_measure = np.append(temp_count_measure, low_measure[0:5])
    temp_count_measure = np.append(temp_count_measure, temp_count_measure[2]+temp_count_measure[7])
    temp_count_measure = np.append(temp_count_measure, temp_count_measure[3]+temp_count_measure[8])
    temp_count_measure = np.append(temp_count_measure, temp_count_measure[4]+temp_count_measure[9])
    temp_count_measure = np.append(temp_count_measure, temp_count_measure[11]/(temp_count_measure[12]+temp_count_measure[13]))
                                   
    temp_pop_measure = np.append(temp_pop_measure, high_measure[5:])
    temp_pop_measure = np.append(temp_pop_measure,low_measure[5:])
    temp_pop_measure = np.append(temp_pop_measure, temp_pop_measure[2]+temp_pop_measure[7])
    temp_pop_measure = np.append(temp_pop_measure, temp_pop_measure[3]+temp_pop_measure[8])
    temp_pop_measure = np.append(temp_pop_measure, temp_pop_measure[4]+temp_pop_measure[9])
    temp_pop_measure = np.append(temp_pop_measure, temp_pop_measure[11]/(temp_pop_measure[12]+temp_pop_measure[13]))

    #np.savetxt("c:/temp/high_measure.csv", temp_h, delimiter=',')
    #np.savetxt("c:/temp/low_measure.csv", temp_l, delimiter=',')
    #print temp_count_measure.shape, temp_pop_measure.shape
    return temp_count_measure, temp_pop_measure

def build_smoothed_list(inputCSV):
    fn = inputCSV
    ra = csv.DictReader(file(fn), dialect="excel")
    i = 0
    for record in ra:
        #print record[ra.fieldnames[0]], type(record[ra.fieldnames[-1]])
        #unit_attri[i,4] = int(float(record[ra.fieldnames[-1]]))
        unit_attri[i,3] = float(record["SmoothedRate"])
        i = i + 1
#--------------------------------------------------------------------------
#MAIN

if __name__ == "__main__":
    print "begin at " + getCurTime()

    H1 = [8,16,844,915,919,921,923,924]
    L2 = [5,103,106,513,517,518,520,531,534,535,536,541]
    H3 = [63,265,267,268,333,336,337,339,340,342,343,348]
    H4 = [13,174,178,198,886,887,888,889,890]
    L5 = [146,171,182,810,811,814,815,864,867]
    L6 = [20,133,692,694,695,696,698,702,705]
    H7 = [69,70,87,88,369,370,372,442,443]
    high_risk_area_id = H1 + H3 + H4 + H7
    low_risk_area_id = L2 + L5 + L6
    
    unitCSV = "C:/TP1000_1m.csv"
    popFN = "pop"
    unit_attri = np.zeros((1000,5))
    dataMatrix = build_data_list()  # [area, pop, cancer1, cancer2, cancer3]
    
    unit_attri[:,1] = dataMatrix[:,1]
    unit_attri[:,2] = dataMatrix[:,0]
    unit_attri[:,4] = np.arange(0,1000)
    #print unit_attri
    #filePath = "C:/_DATA/CancerData/test/Jan15/Thousand/8000/OO_CLK/"
    #filePath = "F:/OO_WARD/"
    j = 0
    while j < 1:
        if j == 0:
            soMethod = "OO"
        elif j == 1:
            soMethod = "SO"
        elif j == 2:
            soMethod = "SS"
        else:
            print "error!"
            break

        filePath = "C:/_DATA/CancerData/test/Jan15/Thousand/6000/"+ soMethod +"_CLK/"
        #filePath = "C:/_DATA/CancerData/test/Jan15/Thousand/max_p/smooth_rate_8000/"
        # returnID used in function find_rep_region
        returnID = 1  # 0: cancer, 1: pop, 2: area
        
        while returnID < 2:
            repeat = 1
            count_measure = np.array([])
            pop_measure = np.array([])
            
            #while repeat < 1001:
            while repeat < 1001:
                regionCSV = filePath + soMethod +"_Full-Order-CLKcancer"+ str(repeat) +".csv"
                #regionCSV = filePath + "cancer"+ str(repeat) +"_smoothed_pop_8000.csv"
                sField_Ob = "cancer" + str(repeat)
                unit_attri[:,0] = dataMatrix[:,repeat+1] # [cancer, pop, area, rate, id]
                riskarea_attri, high_riskarea_attri, low_riskarea_attri = cal_riskare_attri()  # [total_cancer, total_pop, total_area]
                cal_unit_rate()
                #build_smoothed_list(regionCSV)
                #for item in unit_attri:
                    #item[3] = item[0]/item[1]
                
                temp_count_measure, temp_pop_measure = cal_TP()
                count_measure = np.append(count_measure, temp_count_measure)
                pop_measure = np.append(pop_measure, temp_pop_measure)

                repeat = repeat + 1

            count_measure.shape = (-1, 15)
            temp_mean = count_measure.mean(axis=0)
            temp_median = np.median(count_measure, axis=0)
            temp_std = count_measure.std(axis=0)
            count_measure = np.append(count_measure, temp_mean)
            count_measure = np.append(count_measure, temp_median)
            count_measure = np.append(count_measure, temp_std)
            count_measure.shape = (-1, 15)

            pop_measure.shape = (-1, 15)
            temp_mean = pop_measure.mean(axis=0)
            temp_median = np.median(pop_measure, axis=0)
            temp_std = pop_measure.std(axis=0)
            pop_measure = np.append(pop_measure, temp_mean)
            pop_measure = np.append(pop_measure, temp_median)
            pop_measure = np.append(pop_measure, temp_std)
            pop_measure.shape = (-1, 15)
            
            
            print pop_measure[:,1].max(), count_measure[:,1].max(), pop_measure[:,6].max(), count_measure[:,6].max(), pop_measure[:,1].min(),count_measure[:,1].min(), pop_measure[:,6].min(), count_measure[:,6].min()
            #np.savetxt(filePath+"max_p_SS_8000_count_measure2.csv", count_measure, delimiter=',')
            np.savetxt(filePath + soMethod +"_CLK_6000_count_measure2.csv", count_measure, delimiter=',')

            #np.savetxt(filePath+"max_p_SS_8000__pop_measure2.csv", pop_measure, delimiter=',')
            np.savetxt(filePath + soMethod +"_CLK_6000_pop_measure2.csv", pop_measure, delimiter=',')

            returnID = returnID + 1
        j = j + 1

    print "end at " + getCurTime()
    print "========================================================================"  

           
